﻿using LiteDB;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;

namespace litedbdemo
{
    //F:\wcs21_project\com.jt.wcs.ui\data\GlobalSetting.cs
    public class GlobalSetting
    {

        /// <summary>
        ///返回list对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> GetList<T>()
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.FindAll().ToList(); 
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddRang<T>(IList<T> model) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.InsertBulk(model);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string Add<T>(T model) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.Insert(model);
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Update<T>(T model) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.Update(model);
            }
        }

        /// <summary>
        /// 删除，返回影响行数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Delete<T>(BsonValue id) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.Delete(id);
            }
        }
        ///// <summary>
        ///// 5.0不支持此方法
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public static bool Delete<T>(Query query) where T : class
        //{
        //    using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "GlobalSetting.db"))
        //    {
        //        var col = db.GetCollection<T>(typeof(T).Name);
        //        return col.Delete(query);
        //    }
        //}

        /// <summary>
        ///根据条件删除对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static int Delete<T>(Expression<Func<T, bool>> predicate) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.DeleteMany(predicate);
            }
        }
        /// <summary>
        /// 获取单个对象(首个）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T Get<T>() where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.FindAll().FirstOrDefault();
            }
        }
        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T Get<T>(BsonValue id) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.FindById(id);
            }
        }

        /// <summary>
        /// 根据条件获取对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T Get<T>(Query query) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.FindOne(query);
            }
        }

        /// <summary>
        /// 根据条件获取对象列表
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<T> GetList<T>(Query query, int skip = 0, int limit = int.MaxValue) where T : class
        {
            using (var db = new LiteDatabase(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"GlobalSetting.db"))
            {
                var col = db.GetCollection<T>(typeof(T).Name);
                return col.Find(query, skip, limit);
            }
        }
    }
}